<?php
/**
 * Created by JetBrains PhpStorm.
 * User: FangXuanLing
 * Date: 13-11-21
 * Time: 上午10:41
 * To change this template use File | Settings | File Templates.
 */
class CommentController extends Controller{

    //评论列表
    public function actionIndex(){
        $criteria = new CDbCriteria();
        $criteria->order = 'ctime desc';
        $count = Post::model()->count($criteria);

        $pager = new CPagination($count);
        $pager->pageSize = 5;
        $pager->applyLimit($criteria);

        $comm=Comment::model()->with('post')->findAll(array(
            'limit'=>$pager->pageSize,
            'offset'=>$pager->currentPage*$pager->pageSize,
        ));
        //var_dump($comm);die;
        //$this->render('index',array('name'=>'文章列表','post'=>$posts,'pages'=>$pager));
        $this->render('index',array('name'=>'审核列表','comm'=>$comm,'pages'=>$pager));
    }
    //单个审核
    public function actionAudit(){
        $id = Yii::app()->request->getParam('id');
        if(empty($id)){
            $this->redirect(array('admin/comment/index'));
        }
        else{
            //根据id找到数据
            $model = Comment::model()->findByPk($id);
            //审核通过
            $model->status = 1;
            //删除
            $model->save();
            //跳转
            $this->redirect(array('admin/comment/index'));
        }
    }

    //批量审核
    public function actionBatch(){

    }
    //可优化，和单个审核方法可以合并一起处理
    public function actionDel(){
        $id = Yii::app()->request->getParam('id');
        if(empty($id)){
            $this->redirect(array('admin/comment/index'));
        }
        else{
            //根据id找到数据
            $model = Comment::model()->findByPk($id);
            //审核通过
            $model->status = 2;
            //删除
            $model->save();
            //跳转
            $this->redirect(array('admin/comment/index'));
        }
    }
}